﻿using System.Collections.Generic;
using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 4/6/2014

    #region [ Updates ]

    // Author: Ryan Redburn
    // Date: 5/19/2014
    // Revisions: Removed use of course index as means of locating course information (corresponding to SchedulerFacade changes).

    #endregion

    public partial class FormCourseDelete : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;
        private int _courseID;
        private List<string> _courseInfo;

        #endregion

        #region [ Constructors ]

        // Initialize form resources
        public FormCourseDelete(SchedulerFacade facade, int courseID = 0)
        {
            InitializeComponent();
            _facade = facade;

            if (courseID != 0)
                _courseID = courseID;
        }

        #endregion

        #region [ Events ]

        // Display information for the course to be deleted
        private void FormDeleteCourse_Load(object sender, EventArgs e)
        {
            // Display course selected form if no ID is given
            if (_courseID == 0)
            {
                // Open course select form
                using (var frmCourseSelect = new FormCourseSelect(_facade))
                {
                    frmCourseSelect.ShowDialog();

                    // If the user selected a course, retrieve the index value or use the given ID
                    if (frmCourseSelect.DialogResult == DialogResult.OK)
                    {
                        _courseID = frmCourseSelect.SelectionID;
                        _courseInfo = _facade.GetCourseFromTempStore(_courseID);
                    }
                    else
                    {
                        Close();
                    }
                }
            }
            else
            {
                _courseInfo = _facade.GetCourseFromTempStore(_courseID);
            }

            // Display course information
            lblID.Text += " " + _courseInfo[0];
            lblSubNum.Text += " " + _courseInfo[1] + _courseInfo[2];
            lblCourseTitle.Text += " " + _courseInfo[3];
            lblCredits.Text += " " + _courseInfo[4];

            if (_courseInfo[6] == "Yes")
            {
                lblStyle.Text += " Online";
            }
            else if (_courseInfo[7] == "Yes")
            {
                lblStyle.Text += " Hybrid";
            }
            else
            {
                lblStyle.Text += " Classroom";
            }
        }

        // Confirms delete
        private void btnYes_Click(object sender, EventArgs e)
        {
            // Process and display success
            _facade.DeleteCourse(int.Parse(_courseInfo[0]));
            MessageBox.Show("The selected course was successfully deleted.", "Course Deleted");
            Close();
        }

        // Cancels delete
        private void btnNo_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
